<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Cmis File Picker</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" type="text/css" href="bower_components/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="bower_components/fontawesome/css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" href="dist/style.default.min.css" />
    <style type="text/css">
        html,body,#testThis{
            height: 100%;
        }
    </style>
</head>
<body >
<div class="panel-body" id="login-area">
    <form class="cmxform form-horizontal tasi-form" id="cmisForm" method="get" action="" novalidate="novalidate">
        <div class="form-group ">
            <label for="_url" class="control-label col-lg-2">URL</label>
            <div class="col-lg-10">
                <input class="form-control " id="_url" name="url" type="text" ng-model="cmisInfo.url">
            </div>
        </div>
        <div class="form-group ">
            <label for="_token" class="control-label col-lg-2">token</label>
            <div class="col-lg-10">
                <input class=" form-control" id="_token" name="token" type="text" ng-model="cmisInfo.token">
            </div>
        </div>
        <div class="form-group ">
            <label for="_repository" class="control-label col-lg-2">repository</label>
            <div class="col-lg-10">
                <select class="form-control" id="_repository" name="repository" type="" ng-model="cmisInfo.repository">

                </select>
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-2 col-lg-10">
                <button type="button" class="btn btn-primary" onclick="Load_Repository()">Load Repository</button>
            </div>
        </div>
    </form>
</div>
<div id="testThis">

</div>

<script type="text/javascript" src="src/language/da-DK.js"></script>
<script type="text/javascript" src="src/language/de-DE.js"></script>
<script type="text/javascript" src="src/language/en-US.js"></script>
<script type="text/javascript" src="src/language/es-419.js"></script>
<script type="text/javascript" src="src/language/fr-FR.js"></script>
<script type="text/javascript" src="src/language/it-IT.js"></script>
<script type="text/javascript" src="src/language/ja-JP.js"></script>
<script type="text/javascript" src="src/language/ko-KR.js"></script>
<script type="text/javascript" src="src/language/nl-NL.js"></script>
<script type="text/javascript" src="src/language/pl-PL.js"></script>
<script type="text/javascript" src="src/language/pt-PT.js"></script>
<script type="text/javascript" src="src/language/pt-BR.js"></script>
<script type="text/javascript" src="src/language/ru-RU.js"></script>
<script type="text/javascript" src="src/language/fi-FI.js"></script>
<script type="text/javascript" src="src/language/nb-NO.js"></script>
<script type="text/javascript" src="src/language/sv-SE.js"></script>
<script type="text/javascript" src="src/language/zh-CN.js"></script>
<script type="text/javascript" src="src/language/zh-HK.js"></script>
<script type="text/javascript" src="src/language/zh-TW.js"></script>

<script type="text/javascript" src="bower_components/FileSaver/FileSaver.min.js"></script>
<script type="text/javascript" src="bower_components/base64/base64.min.js"></script>
<script type="text/javascript" src="bower_components/superagent/superagent.js"></script>
<script type="text/javascript" src="bower_components/smx-cmis/lib/cmis.js"></script>
<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
<script type="text/javascript" src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script type="text/javascript" src="bower_components/angular-local-storage/dist/angular-local-storage.js"></script>
<script type="text/javascript" src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<script type="text/javascript" src="bower_components/angular-translate/angular-translate.js"></script>
<script type="text/javascript" src="bower_components/angular-toastr/dist/angular-toastr.tpls.js"></script>
<script type="text/javascript" src="lib/ng-context-menu.js"></script>
<script type="text/javascript" src="bower_components/angular-file-model/angular-file-model.js"></script>
<script type="text/javascript" src="bower_components/clipboard/dist/clipboard.js"></script>
<script type="text/javascript" src="bower_components/ngclipboard/dist/ngclipboard.js"></script>
<script type="text/javascript" src="bower_components/ngInfiniteScroll/build/ng-infinite-scroll.js"></script>
<script type="text/javascript" src="bower_components/angular-animate/angular-animate.js"></script>
<script type="text/javascript" src="bower_components/jquery-qrcode/jquery.qrcode.min.js"></script>
<script type="text/javascript" src="bower_components/clipboard/dist/clipboard.js"></script>

<!-- develop -->
<script type="text/javascript" src="src/cmis-directive.js"></script>
<script type="text/javascript" src="src/service.js"></script>
<script type="text/javascript" src="src/controllers.js"></script>
<script type="text/javascript" src="src/models.js"></script>
<script type="text/javascript" src="src/main-controller.js"></script>
<script type="text/javascript" src="src/modal-controller.js"></script>
<script type="text/javascript" src="src/filter.js"></script>
<script type="text/javascript" src="src/directives/file-dropzone.js"></script>
<script type="text/javascript" src="src/directives/drag-and-drop.js"></script>
<script type="text/javascript" src="src/directives/selection-area.js"></script>
<script type="text/javascript" src="src/directives/breadcrumb.js"></script>
<script type="text/javascript" src="src/directives/common.js"></script>



<script type="text/javascript">

    // 填写cmis的url+token,选择仓库
    $("#_token").mouseleave(function (){
        var url=$("#_url");
        var token=$("#_token");
        $.ajax({
            method: "GET",
            url: url.val(),
            headers: {"Authorization": "Bearer " + token.val()},
            dataType: "json",
            success:function(data){
                for(var key in data) {
                    $("#_repository").append("<option>" + key + "</option>");
                }
            }
        })
    });

    urlParaToObj = function(urlPara, needEncode) {
        var arr, i, k, len1, obj;
        obj = {};
        if (urlPara.indexOf('&') !== -1) {
            arr = urlPara.split('&');
            for (k = 0, len1 = arr.length; k < len1; k++) {
                i = arr[k];
                if (needEncode) {
                    obj[i.split('=')[0]] = encodeURIComponent(i.split('=')[1]);
                } else {
                    obj[i.split('=')[0]] = i.split('=')[1];
                }
            }
        } else {
            if (needEncode) {
                obj[urlPara.split('=')[0]] = encodeURIComponent(i.split('=')[1]);
            } else {
                obj[urlPara.split('=')[0]] = urlPara.split('=')[1];
            }
        }
        return obj;
    };
    getArgsFromUrl = function(url) {
        var index, result, urlParas;
        if (!url) {
            url = location.href;
        }

        if(url.indexOf('#')>-1){
            url=url.split('#')[0]
        }

        index = url.indexOf('?');
        if (index === -1) {

        } else {
            urlParas = url.slice(index + 1);
            result = urlParaToObj(urlParas);
            console.log(result);
            return result;
        }
    };
    function initView(url,token,path,repository,baseUrl){
        if(!path){
            path='/';
        }
        var cmisTag = '<cmis url="'+ url +'" token="'+ token +'" ' +
                'path="'+ path +'" ' +
                'is-drm-room="false" ' +
                'show-members="true" ' +
                'message-to-member="" ' +
                'room-link="" ' +
                'membership-type="Owner" ' +
                'lang=""' +
                'repository = "'+ repository +'"' +
                'file-picker-modal=""' +
                'device="foxitReader" ' +
                'base-url="'+ baseUrl +'"'+
                '>'+
                '</cmis>';
        $('#testThis').empty().append(cmisTag);
        $('#login-area').hide();
        $('#testThis').show();
        angular.module('test', ['UCX.Cmis']);
        angular.element(document).ready(function () { angular.bootstrap(document, ['test']); });
    }
    function Load_Repository(fromUrl){
        var url,token,repository,baseUrl,path,baseUrlIndex;
        var urlParams = getArgsFromUrl();
        console.log(urlParams);

//    from url
        if(urlParams && urlParams.token && urlParams.repository && urlParams.url){
            url= decodeURIComponent(urlParams.url);
            token= decodeURIComponent(urlParams.token);
            repository=decodeURIComponent(urlParams.repository);
            baseUrlIndex =urlParams.url.indexOf('/',10);
            path=urlParams.path && decodeURIComponent(urlParams.path) || '/';
            baseUrl = decodeURIComponent(urlParams.url.slice(0,baseUrlIndex+1));
            initView(url,token,path,repository,baseUrl)
        }else{
            if(fromUrl){
                return
            }
//    from login
            url=$("#_url").val();
            token=$("#_token").val();
            repository=$("#_repository").val();
            baseUrlIndex = $("#_repository").val().indexOf('/',10);
            baseUrl = $("#_repository").val().slice(0,baseUrlIndex+1);
            path='/';
            location.href=location.href+"?token="+token+"&url="+encodeURIComponent(url)+"&repository="+encodeURIComponent(repository)+"&baseUrl="+encodeURIComponent(baseUrl)+"&path="+encodeURIComponent(path);
            initView(url,token,path,repository,baseUrl)
        }
    }
    Load_Repository(true);


</script>

</body>
</html>


